package com.cn.wanxi.service.impl;

import com.cn.wanxi.data.UserData;
import com.cn.wanxi.data.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.List;

public class UserServletImpl implements IUserService {
    @Override
    public String register(UserModel userModel) {
        UserData userData = new UserImpl();
        UserModel model = userData.findByUsername(userModel.getUsername());
        if (model != null) {
            return "isHave";
        } else {
            int result = userData.register(userModel);
            return result == 1 ? "success" : "error";
        }

    }

    /**
     * 判断输入的验证码是否正确
     */
    @Override
    public String login(UserModel userModel) {
        String result = "";
        if (!userModel.getCode().equals(userModel.getSessionCode())) {
            result = "error_code";
        } else {
            UserData userData = new UserImpl();
            UserModel model = userData.findByUsernameAndPassword(userModel);
            if (model != null) {
//                返回为空值
                result = "success";
            } else {
                result = "error";
            }
        }
        return result;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        UserData userData = new UserImpl();
        return userData.findAll(userModel);
    }

    @Override
    public String del(Integer id) {
        UserData userData = new UserImpl();
        return userData.del(id) == 1 ? "success" : "error";
    }

    @Override
    public UserModel findById(Integer id) {
        UserData userData = new UserImpl();
        return userData.findById(id);
    }

    @Override
    public String edit(UserModel userModel) {
        UserData userData = new UserImpl();
        UserModel model = userData.findByUsernameForEdit(userModel);
        if (model != null) {
            return "ishave";
        } else {

            return userData.edit(userModel) == 1 ? "success" : "error";
        }

    }
}
